<?php

namespace Web\MainSiteBundle\Controller;

use Model\CourseBundle\Model\CourseModel;
use Model\CourseLikesBundle\Model\CourseLikesModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
 * @route("CourseLikes")
 */
class CourseLikesController extends WebBaseController
{
    /**
     * @route("/add",name="WebCourseLikesAdd")
     */
    public function WebCourseLikesAdd()
    {
        if (!$whom = $this->getParamRequired("id")) {$this->showMessagePage("数据丢失，请刷新后重试");}
        $user = $this->needToBeUser();
        $userId = $user->getId();
        $CourseLikesModel = new CourseLikesModel($this->getDoctrine());
        $courseModel = new CourseModel($this->getDoctrine());
        $course = $courseModel->getOneById($whom,$userId);
        $uniqueId = $course["uniqueId"];
        if ($CourseLikesModel->chkIfLikes($userId,$whom)) {
            return $this->js_out("已赞",$this->generateUrl("WebCourseIndex",array("uniqueId"=>$uniqueId)));
        } else {
            $params = array(
                "who"   => $userId,
                "whom"  => $whom,
            );
            $CourseLikesModel->commonAdd($params);
            return $this->js_out("已赞",$this->generateUrl("WebCourseIndex",array("uniqueId"=>$uniqueId)));
        }
    }

    /**
     * @route("/del",name="WebCourseLikesDel")
     */
    public function WebCourseLikesDel()
    {
        $user = $this->needToBeUser();
        $userId = $user->getId();
        if (!$whom = $this->getParamRequired("id")) {$this->showMessagePage("数据丢失，请刷新后重试");}
        $commentsModel = new CourseLikesModel($this->getDoctrine());
        $commentsModel->delete($userId,$whom);
        $courseModel = new CourseModel($this->getDoctrine());
        $course = $courseModel->getOneById($whom,$userId);
        $uniqueId = $course["uniqueId"];
        return $this->js_out("已取消赞",$this->generateUrl("WebCourseIndex",array("uniqueId"=>$uniqueId)));
    }
}
